#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2021/7/2 16:31
# @Author    :cjw
from time import sleep

import allure
from selenium.common.exceptions import NoSuchElementException

from app_wework_po.util.log import logger
from app_wework_po.setting import ELEMENT_YML
from app_wework_po.base.base_page import BasePage
from app_wework_po.util.file_operator import YmlOperator
from app_wework_po.util.handle_uiselector import handle_result_tuple


class ContactListPage(BasePage):
	"""通讯录页面"""
	# 添加元素定位数据到locators
	BasePage.locators.update(YmlOperator(ELEMENT_YML['clp']).data)

	def goto_add_member(self):
		"""跳转到添加成员页面"""
		from app_wework_po.page.add_member_page import AddMemberPage

		with allure.step(msg := '点击“添加成员”，进入添加成员页面'):
			logger.info(msg)
			# 滑动查找，找到后点击
			# ele = self.swipe_find(tuple(self.locators['add_member']))
			ele = self.swipe_find(handle_result_tuple(self.locators['add_member'][1]))
			self.tap(ele)
		return AddMemberPage(self.driver)

	def goto_person_information(self, name: str):
		"""
		跳转到个人信息页面
		:param name: 个人姓名
		:return:
		"""
		from app_wework_po.page.person_information_page import PersonInformationPage

		# 补充个人姓名定位表达式的值
		loc_value = self.locators['person_name'][1]['value']
		loc_value.append(name)

		with allure.step(msg := f'下滑查找“{name}，”点击“{name}”，跳转到{name}个人信息页面'):
			logger.info(msg)
			# 滑动查找，找到后点击。由于查找个人名称已经上滑到底部了，所以进行下滑查找
			ele = self.swipe_find(handle_result_tuple(self.locators['person_name'][1]), direction='down')
			self.tap(ele)
		return PersonInformationPage(self.driver)

	def person_names(self) -> set:
		"""
		分别从添加客户往下查找，添加成员往上查找，查找了两次当前页，所以名称不能过多，否则查找不全
		:param search_num: 滑动屏幕查找次数
		:return:
		"""
		names_text = set()

		# 添加客户往下查找当前页显示的成员
		with allure.step(msg := '下滑动到“添加客户”，开始从“添加客户”往下进行个人姓名获取'):
			logger.info(msg)
			self.swipe_find(handle_result_tuple(self.locators['add_customer'][1]), direction='down')
			texts = {ele.text for ele in self.name_list_following_customer
			         if ele.text not in {'紫陌翌晨', '企业通讯录', '添加成员'}
			         and '部' not in ele.text}
			names_text.update(texts)

		# 添加成员往上查找当前页显示的成员
		with allure.step(msg := '上滑动到“添加成员”，开始从“添加成员”往上进行个人姓名获取'):
			logger.info(msg)
			self.swipe_find(handle_result_tuple(self.locators['add_member'][1]))
			texts = {ele.text for ele in self.name_list
			         if ele.text not in {'紫陌翌晨', '添加客户', '企业通讯录', '我的客户'}
			         and '部' not in ele.text}
			names_text.update(texts)
		with allure.step(msg := f'获取到个人姓名集合: {names_text}'):
			logger.info(msg)
		return names_text
